#define BLINKER_WIFI    //配置为WIFI接入模式
#define BLINKER_MIOT_OUTLET  //配置为小米插座类设备

#include <Blinker.h>      //Blinker库
#include <ESP8266WiFi.h>    //WIFI库，配网必需

int count = 0;     //时间计数
bool WIFI_Status = true;   //WIFI状态标志位

#define pin D1	       //要操作的IO
char auth[] = "xxxxxxxx";  //设备秘钥
bool oState = false;
/* 配网函数 */
void smartConfig()
{
  WiFi.mode(WIFI_STA);//设置STA模式
  Serial.println("\r\nWait for Smartconfig...");//打印log信息
  WiFi.beginSmartConfig();//开始SmartConfig，等待手机端发出用户名和密码
  while(1)
  {
    Serial.println(".");
    digitalWrite(LED_BUILTIN,HIGH);//指示灯闪烁
    delay(1000);
    digitalWrite(LED_BUILTIN,LOW);//指示灯闪烁
    delay(1000);
    if(WiFi.smartConfigDone())//配网成功，接收到SSID和密码
    {
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      break;      
    }
  }  
}

/* 小米电源类设备回调函数 */
void miotPowerState(const String & state)
{
    BLINKER_LOG("need set power state: ", state);
    if (state == BLINKER_CMD_ON) {     //指令打开设备
        digitalWrite(LED_BUILTIN, HIGH);
        digitalWrite(pin, HIGH);
        BlinkerMIOT.powerState("on");
        BlinkerMIOT.print();

        oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {   //指令关闭设备
        digitalWrite(LED_BUILTIN, LOW);
        digitalWrite(pin, LOW);
        BlinkerMIOT.powerState("off");
        BlinkerMIOT.print();

        oState = false;
    }
}
/* 小米设备状态查询回调函数 */
void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

    switch (queryCode)
    {
        case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("MIOT Query All");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
        case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("MIOT Query Power State");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
        default :
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
    }
}

void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);

    Blinker.vibrate();
    
    uint32_t BlinkerTime = millis();
    
    Blinker.print("millis", BlinkerTime);
}

void setup()
{
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    
    pinMode(pin, OUTPUT);
    digitalWrite(pin, LOW); 
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println("\r\n正在连接WIFI...");

    while(WiFi.status()!=WL_CONNECTED)//判断是否连接WIFI成功
    {
      if(WIFI_Status)
      {
          Serial.print(".");
          digitalWrite(LED_BUILTIN, HIGH);  
          delay(500);                       
          digitalWrite(LED_BUILTIN, LOW);    
          delay(500);                 
          count++;
          if(count>=5)//5s
          {
              WIFI_Status = false;
              Serial.println("WiFi连接失败，请用手机进行配网"); 
          }        
        }
        else
        {
          smartConfig();  //微信智能配网
        }
    }

    Serial.println("连接成功");  
    Serial.print("IP:");
    Serial.println(WiFi.localIP());
   
    Blinker.begin(auth,WiFi.SSID().c_str(),WiFi.psk().c_str()); //根据配网得到的WIFI信息和设备秘钥，连接到Blinker云
    Blinker.attachData(dataRead);
   
    BlinkerMIOT.attachPowerState(miotPowerState);//注册回调函数
    BlinkerMIOT.attachQuery(miotQuery);
}

void loop()
{
    Blinker.run();
}